import Vue from 'vue'
import VueRouter from 'vue-router'
import Frame from '../src/components/frame.vue'
import Login from '../src/components/login.vue'
import { RouterTabRoutes } from 'vue-router-tab'

const importPage = (view) => () => import(`../page/${view}.vue`)

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Frame,
    meta: {
      title: '主页', // 页签标题
    },

    children: [
      // 引入 RouterTab 内置路由以支持 iframe 页签
      ...RouterTabRoutes,

      {
        path: '/', // 默认页和父级路由一致
        name: 'index',
        component: importPage('index'),
        meta: {
          title: '主页', // 页签标题
        },
      },
      {
        path: '/user',
        name: 'user',
        component: importPage('user/list'),
        meta: {
          title: '用户管理',
        },
      },
      {
        path: '/user/new',
        name: 'user_new',
        component: importPage('user/new'),
        meta: {
          title: '新增用户',
        },
      },
      {
        path: '/user/detaile',
        name: 'user_detaile',
        component: importPage('user/detaile'),
        meta: {
          title: '修改信息',
        },
      },
      {
        path: '/machine',
        name: 'machine',
        component: importPage('machine/list'),
        meta: {
          title: '设备列表',
        },
      },
      {
        path: '/machine/detaile',
        name: 'machine_detaile',
        component: importPage('machine/detaile'),
        meta: {
          title: '修改信息',
        },
      },
      {
        path: '/machine/new',
        name: 'machine_new',
        component: importPage('machine/new'),
        meta: {
          title: '新增设备',
        },
      },
      {
        path: '/program',
        name: 'program',
        component: importPage('program/list'),
        meta: {
          title: '项目列表',
        },
      },
      {
        path: '/program/detail',
        name: 'program_detail',
        component: importPage('program/detail'),
        meta: {
          title: '修改项目信息',
        },
      },
      {
        path: '/program/new',
        name: 'program_new',
        component: importPage('program/new'),
        meta: {
          title: '新增项目',
        },
      },
      {
        path: '/watch',
        name: 'watch',
        component: importPage('watch/list'),
        meta: {
          title: '数据监控',
        },
      },
      {
        path: '/allwatch',
        name: 'allwatch',
        component: importPage('allwatch/list'),
        meta: {
          title: '历史数据',
        },
      },

      {
        path: '/page/:id',
        name: 'Page',
        component: importPage('Page'),
        meta: {
          key: 'path',
          title(route) {
            return `Page ${route.params.id}`
          },
        },
      },
    ],
  },
  {
    path: '/',
    name: 'login',
    component: Login,
  },
]
const router = new VueRouter({
  routes,
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

export default router
